<?
	include_once('header.php');

	include_once('sidebar.php');

	$edit = $_GET['edit'];
	$offset = $_GET['offset'];
	$limit = $_GET['limit'];
	$action = $_GET['action'];
	if(!$offset) { $offset = "0"; }
	if(!$limit) { $limit = "20"; }
	$delete = $_GET['delete'];
?>

		
		<div id="main">
			
<?
	if(!drizzo_user_is_allowed('blog-write') || drizzo_global_options('module_blog') != 1) {
		echo "<h2>Pages</h2><br class=\"clear\" /><p>This module is not available.</p><br class=\"clear\" />";
		include_once('footer.php');
		exit();
	}
	if($delete) {
?>
			<h2>Delete a Post</h2>
			<br class="clear" />
<?
		if(!isset($_POST['delete_check'])) { // Display a confirmation
			$the_post = drizzo_get_the_post($delete);
			if(drizzo_post_permissions_check()) {
?>
			<p>Are you sure you want to send <? echo $the_post['title']; ?> to the TrashHeap?</p>
			<form action="blog.php?delete=<? echo $delete; ?>" method="post" name="deleter" id="deleter">
				<input type="hidden" name="delete_check" value="1"></input>
				<input type="hidden" name="delete_id" value="<? echo $delete; ?>"></input>
				<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
<?			
			} else {
				echo "<p class=\"alert\">You do not have permission to delete this page.</p>";
			}
		} else { // Move the post to the trashheap
			$trashheap = drizzo_move_to_trashheap($_POST['delete_id']);
			if($trashheap) {
					echo "<p class=\"trash\">The post has been piled on the TrashHeap.</p>";
			} else {
				echo "<p class=\"alert\">There was a problem and the post could not be moved to the TrashHeap.<br />" . mysql_error() . "</p>";
			}
		}
	} // End Delete

	if($action == "publish") { // Insert data and set to publish
		
		 if($_POST) {
			$insert = drizzo_post_save();
			if($insert) { // If insert is succesful
				echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['title']) . " has been saved.</p>";
			} else { // If insert fails
				echo "<p class=\"alert\">There has been a database error. " . drizzo_db_unwrap($_POST['title'])  . " has not been saved.<br />" . mysql_error() . "</p>";
			}
		} else { // If there is no Post data
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		} // End $_POST
	} // End publish

	if(!isset($edit) && ($action != "continue") && ($action != "new") && !$delete && ($action != "continue_new")) { // Default View for non drafts setup

		drizzo_db_connect();
	
		if(!$action || ($action == "publish")) {
			$prev_offset = ($offset + $limit);
			$next_offset = abs(($offset - $limit));
			$total_count = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`!='draft' AND `type` IN ('blog','aside') ORDER BY `posted_date`,`ID` ASC");
			$total_count = @mysql_num_rows($total_count);
			if($prev_offset < $total_count) { $show_prev = "true"; }
			if($offset !=0) { $show_next = "true"; }
		
			$the_posts = drizzo_get_the_posts($limit, $offset);
?>
			<h2>Current Posts</h2>
			<br class="clear" />
<?
		} // End Default View setup
	
		if($action == "view_drafts") { // View drafts setup
			$prev_offset = ($offset + $limit);
			$next_offset = abs(($offset - $limit));
			$total_count = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status`='draft' and `type` IN ('blog','aside') ORDER BY `posted_date`,`ID` ASC");
			$total_count = @mysql_num_rows($total_count);
			if($prev_offset < $total_count) { $show_prev = "true"; }
			if($offset !=0) { $show_next = "true"; }
		
			$the_posts = drizzo_get_the_posts($limit, $offset, "draft");
?>
			<h2>Draft Posts</h2>
			<br class="clear" />
<?
		} // End drafts view setup
	
		if(!$action || ($action == "view_drafts") || ($action == "publish")) { // Default view table of posts
		
			if(($show_prev == "true") || ($show_next == "true")) {  // Previous & Next top navigation
				echo "<p class=\"right\" id=\"navigation\">";
			}
			if($show_prev == "true") {
				if(drizzo_global_options('backend_ajax')) {
?>
				<a href="javascript:PostTableUpdate('posts_table',<? if($action == 'view_drafts') { echo "'drafts',"; } else { echo "'publish',"; } echo "'$limit','$prev_offset'"; ?>,'table_wrap','<? echo $total_count; ?>')" title="Earlier Posts">&laquo; Earlier Posts</a>
<?					
				} else {
					echo "<a href=\"blog.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
				}
			}
			if(($show_prev == "true") && ($show_next == "true")) {
				echo " | ";
			}
			if($show_next == "true") {
				if(drizzo_global_options('backend_ajax')) {
?>					
				<a href="javascript:PostTableUpdate('posts_table',<? if($action == 'view_drafts') { echo "'drafts',"; } else { echo "'publish',"; } echo "'$limit','$next_offset'"; ?>,'table_wrap','<? echo $total_count; ?>')" title="Earlier Posts">&laquo; Earlier Posts</a>
<?
				} else {
					echo "<a href=\"blog.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
				}
			}
			if(($show_prev == "true") || ($show_next == "true")) {
				echo "</p>";
			}
	}
?>			
			<div id="table_wrap">
			<table id="blog_posts">
				<tr id="first">
					<th class="center">ID</th><th>Title</th><th>Tags</th><th>Created</th><th class="center">Comments</th><th class="center">Author</th><th class="center" colspan="3">Action</th>
				</tr>
<?
			if(count($the_posts) > 0) {
				foreach($the_posts as $the_post) {
					extract($the_post);
					if ($alt_count%2) { 
						echo "<tr class=\"alt\">\n";
					} else {
						echo "<tr>\n";
					}
					echo "\t<td class=\"center\">" . $ID . "</td>";
					if($the_post['type'] == 'aside') {
						echo "<td class=\"aside\">" . $title . "</td>";
					} else {
						echo "<td>" . $title . "</td>";
					}
					echo "<td>" . drizzo_post_tags() . "</td><td>" . $posted_date . "</td><td class=\"center\" width=\"1\">" . $comment_count . "</td><td class=\"center\" width=\"1\">" . drizzo_post_author('nice_name') . "</td><td class=\"center\" width=\"1\">" . drizzo_view_link_backend() . "</td><td class=\"center\" width=\"1\">" . drizzo_edit_link_backend() . "</td><td class=\"center\" width=\"1\">" . drizzo_delete_link_backend() . "</td>\n";
					echo "</tr>\n";
					$alt_count++;
				}
			}
						
			if(count($the_posts) == 0) {
				echo "<tr>\n";
				echo "\t<td colspan=\"9\">No Posts were found.</td>\n";
				echo "</tr>\n";
			}
?>					
			</table>
			</div>
<?
			if(($show_prev == "true") || ($show_next == "true")) {  // Previous & Next bottom navigation
				echo "<p class=\"right\" id=\"navigation\">";
			}
			if($show_prev == "true") {
				if(drizzo_global_options('backend_ajax')) {
?>
				<a href="javascript:PostTableUpdate('posts_table',<? if($action == 'view_drafts') { echo "'drafts',"; } else { echo "'publish',"; } echo "'$limit','$prev_offset'"; ?>,'table_wrap','<? echo $total_count; ?>')" title="Earlier Posts">&laquo; Earlier Posts</a>
<?					
				} else {
					echo "<a href=\"blog.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
				}
			}
			if(($show_prev == "true") && ($show_next == "true")) {
				echo " | ";
			}
			if($show_next == "true") {
				if(drizzo_global_options('backend_ajax')) {
?>					
				<a href="javascript:PostTableUpdate('posts_table',<? if($action == 'view_drafts') { echo "'drafts',"; } else { echo "'publish',"; } echo "'$limit','$next_offset'"; ?>,'table_wrap','<? echo $total_count; ?>')" title="Earlier Posts">&laquo; Earlier Posts</a>
<?
				} else {
					echo "<a href=\"blog.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
				}
			}
			if(($show_prev == "true") || ($show_next == "true")) {
				echo "</p>";
			}
?>
			<br class="clear" />
<?
		} // End Default View

	if(($action == "continue_new") && $_POST) {
		if($_POST) {
			$insert = drizzo_post_save();

			if($insert) {
				echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['title']) . " has been saved. You may continue editing.</p>";
			} else {
				echo "<p class=\"alert\">There has been a database error. " . drizzo_db_unwrap($_POST['title']) . " has not been saved.<br />" . mysql_error() . "</p>";
			}

			echo $alert;

			$the_post = drizzo_get_the_post($insert);
		} else {
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		}

	} // End $action == continue_new
	
	if($edit) {

		if(($action == "continue") && $_POST) {
			if($_POST) {
				$update_sql = drizzo_post_save();
				if($update_sql) { // Give user feedback and move old data to the warehouse
					echo "<p class=\"save\">" . $title . " has been saved. You may continue editing.</p>";
				} else {
					echo "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
				}
			} else {
				echo "<p class=\"alert\">No post was being worked on.</p>";
			}
		} // End $action == continue
		
		$the_post = drizzo_get_the_post($edit);

	} // End $edit
	
	if($edit || (($action == "continue_new") && $_POST)) {
		if(!drizzo_post_permissions_check()) {
			echo "<p class=\"alert\">You do not have permission to edit this post.</p>";
		} else {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
				
				<label for="title">Title</label><input name="title" type="text" value="<? echo drizzo_post_title(); ?>"></input><br />

				<label for="content">Content</label><textarea name="content" id="post_content"><? echo drizzo_post_content_backend(); ?></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"><? echo drizzo_post_tags($edit, "comma", "false"); if(drizzo_post_tags($edit)) { echo ", "; }?></textarea><br />
				
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<label for="date">Date</label>	<? echo drizzo_date_dropdowns(); ?>

				<input type="hidden" name="ID" value="<? echo $edit; ?>">
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'blog.php?edit=<? echo drizzo_post_id(); ?>&action=continue'" value="Save &amp; Continue Editing" class="submit"></input><input type="submit" name="edit_submit" onclick="document.editor.action = 'blog.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>
			
			<div id="options">

				<h3 id="file_list_header">Uploaded Media</h3>
				<div id="confirm_image">
					<p>Do you want to insert a link to the image with a thumbnail or place the full image directly?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertImageLink('thumb')" title="Thumbnail Link">Thumbnail Link</a> | <a href="javascript:void(0)" onclick="insertImageLink('image')" title="Full Size">Full Size</a> | <a href="javascript:void(0)" onclick="$('confirm_image').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="confirm_embed">
					<p>Do you want to insert a download link to the file or embed it to play on the page?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertMediaLink('link')" title="Download Link">Download Link</a> | <a href="javascript:void(0)" onclick="insertMediaLink('embed')" title="Embed">Embed</a> | <a href="javascript:void(0)" onclick="$('confirm_embed').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="file_list_div">
					<script type="text/javascript" charset="utf-8">
						getFolderContents('drizzo-files');
					</script>
				</div>
				
				<? drizzo_post_authors_dropdown(); ?>
				
				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>
				
				<? drizzo_post_comment_statuses_dropdown(); ?>
				
				<? drizzo_post_permalink_name_input(); ?>
				
				<? drizzo_post_eval_content_dropdown(); ?>

				<? drizzo_edit_status(); ?>
				
				<? if($edit) { echo "<h3>Previous Revisions</h3>\n" . drizzo_warehouse_revisions_list(); } ?>

			</div>

			</form>
<?
		}
	} // End $edit || $action == continue_new
	
	if($action == "new") {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
	
				<label for="title">Title</label><input name="title" type="text" value=""></input><br />

				<label for="content">Content</label><textarea name="content" id="post_content"></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
	
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<div id="new_date">
					<label for="date">Date</label>	<? echo drizzo_date_dropdowns(); ?>
				</div>

				<p></p>
				
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'blog.php?action=continue_new'" value="Save Draft" class="submit"></input><input type="submit" name="new_submit" onclick="document.editor.action = 'blog.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>

			<div id="options">
				
				<h3 id="file_list_header">Uploaded Media</h3>
				<div id="confirm_image">
					<p>Do you want to insert a link to the image with a thumbnail or place the full image directly?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertImageLink('thumb')" title="Thumbnail Link">Thumbnail Link</a> | <a href="javascript:void(0)" onclick="insertImageLink('image')" title="Full Size">Full Size</a> | <a href="javascript:void(0)" onclick="$('confirm_image').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="confirm_embed">
					<p>Do you want to insert a download link to the file or embed it to play on the page?</p>
					<p class="center"><a href="javascript:void(0)" onclick="insertMediaLink('link')" title="Download Link">Download Link</a> | <a href="javascript:void(0)" onclick="insertMediaLink('embed')" title="Embed">Embed</a> | <a href="javascript:void(0)" onclick="$('confirm_embed').style.display = 'none'" title="Cancel">Cancel</a></p>
				</div>
				<div id="file_list_div">
					<script type="text/javascript" charset="utf-8">
						getFolderContents('drizzo-files');
					</script>
				</div>
				
				<? drizzo_post_authors_dropdown(session_user_id); ?>
	
				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>
	
				<? drizzo_post_comment_statuses_dropdown(); ?>
	
				<? drizzo_post_permalink_name_input(); ?>

				<? drizzo_post_eval_content_dropdown(); ?>
	
			</div>

			</form>
<?
	} // End $action == new
?>
			<br class="clear" />

		</div>
<?
	if($edit) {
?>		
		<div id="preview_wrap">
			
			<div id="preview">
			
				<h2>Post Preview</h2>
				<br class="clear" />
			
<? if(strlen($the_post['content'])>0) { echo drizzo_texturize(drizzo_db_unwrap($the_post['content'])); } ?>

			</div>

		</div>
<?
	}
	include_once('footer.php');
?>
